package com.xiaochen.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochen.manager.service.ProductService;
import com.xiaochen.model.dto.product.ProductDto;
import com.xiaochen.model.entity.product.Product;
import com.xiaochen.model.vo.common.Result;
import com.xiaochen.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品管理
 * @author pinkhors
 */
@RestController
@RequestMapping("/admin/product/product")
public class ProductController {

    @Autowired private ProductService productService;

    /**
     * 1.分页查询
     */
    @GetMapping("/selectByPage/{pageNum}/{pageSize}")
    public Result selectByPage(ProductDto productDto,
                               @PathVariable(name = "pageNum") Integer pageNum,
                               @PathVariable(name = "pageSize") Integer pageSize){
        PageInfo<Product> productPageInfo = productService.selectByPage(productDto,pageNum,pageSize);
        return Result.build(productPageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 2.添加
     */
    @PostMapping("/saveProduct")
    public Result saveProduct(@RequestBody Product product){
        productService.saveProduct(product);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据商品ID查询商品信息
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id){
        Product product = productService.selectById(id);
        return Result.build(product, ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存修改的数据
     */
    @PutMapping("updateById")
    public Result updateById(@RequestBody Product product){
        productService.updateById(product);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 删除
    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        productService.deleteById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    // 审核
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    // 商品的上下架
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }






}
